package bb.lanxing.mvp.presetner;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Parcelable;

import bb.lanxing.R;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.mvp.view.IRecommendationLushuDetailView;
import bb.lanxing.activity.routebook.RouteBookInfoActivity;
import bb.lanxing.network.RxJavaHttpClient;
import bb.lanxing.util.LushuStyleUtils;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

public class RecommendationDetailPresenter extends AbsPresenter<IRecommendationLushuDetailView> {
    private final String KEY_CAN_LIKE;

    @Override
    public void bind(IRecommendationLushuDetailView iRecommendationLushuDetailView) {
        super.bind(iRecommendationLushuDetailView);
    }

    @Override
    public boolean onBackPressed() {
        return super.onBackPressed();
    }

    @Override
    public void unbind() {
        super.unbind();
    }

    public RecommendationDetailPresenter(IRecommendationLushuDetailView iRecommendationLushuDetailView) {
        super(iRecommendationLushuDetailView);
        this.KEY_CAN_LIKE = "key_can_like_";
    }

    public boolean like(final long j) {
        if (mView == null) {
            return false;
        }
        if (!canLike(j)) {
            mView.toast("不能重复点赞");
            return false;
        }
        (this.mView).disableLike();
        RxJavaHttpClient.postLushuCollectionLike(j).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
                if (mView != null) {
                    mView.enableLike();
                    mView.toast(R.string.str_lushu_like_successful);
                }
            }

            @Override
            public void onError(Throwable th) {
                if (mView != null) {
                    mView.toast(R.string.str_lushu_like_fail);
                    mView.refreshLikeView(false);
                    mView.enableLike();
                }
            }

            @Override
            public void onNext(Boolean bool) {
                if (mView != null) {
                    mView.disableLike();
                    mView.refreshLikeView(bool);
                }
            }
        });
        return true;
    }

    public void showLushuDetail(RouteBook routeBook) {
        if (mView == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setComponent(new ComponentName((this.mView).getPackageName(),
                RouteBookInfoActivity.class.getName()));
        intent.putExtra("lushu", (Parcelable) routeBook);
        LushuStyleUtils.installStyle(intent, routeBook.getStyle());
        (this.mView).startActivity(intent);
    }

    public boolean canLike(long j) {
        SharedManager sharedManager = SharedManager.getInstance();
        return sharedManager.getBoolean("key_can_like_" + j, true);
    }
}
